using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
#if UNITY_WEBGL && !UNITY_EDITOR
using WeChatWASM;
#endif

public class UGUIInput : InputField, IPointerClickHandler, IPointerExitHandler
{
    public override void OnPointerClick(PointerEventData eventData)
    {
#if UNITY_WEBGL && !UNITY_EDITOR
        WX.ShowKeyboard(new ShowKeyboardOption()
        {
            defaultValue = text,
            maxLength = characterLimit,
            confirmType = "go"
        });
        //绑定回调
        WX.OnKeyboardConfirm(OnConfirm);
        WX.OnKeyboardComplete(OnComplete);
        WX.OnKeyboardInput(OnInput);
#else
        base.OnPointerClick(eventData);
#endif
    }

    public override void OnPointerExit(PointerEventData eventData)
    {
#if UNITY_WEBGL && !UNITY_EDITOR
        // 隐藏输入法
        if (!isFocused)
        {
            WX.HideKeyboard(new HideKeyboardOption());
            //删除掉相关事件监听
            WX.OffKeyboardInput(OnInput);
            WX.OffKeyboardConfirm(OnConfirm);
            WX.OffKeyboardComplete(OnComplete);
        }
#else
        base.OnPointerExit(eventData);
#endif
    }

#if UNITY_WEBGL && !UNITY_EDITOR
    public void OnInput(OnKeyboardInputListenerResult v)
    {
        Debug.Log("onInput");
        Debug.Log(v.value);
        if (isFocused)
        {
            text = v.value;
        }

    }

    public void OnConfirm(OnKeyboardInputListenerResult v)
    {
        // 输入法confirm回调
        Debug.Log("onConfirm");
        Debug.Log(v.value);
    }

    public void OnComplete(OnKeyboardInputListenerResult v)
    {
        // 输入法complete回调
        Debug.Log("OnComplete");
        Debug.Log(v.value);
    }
#endif

}